package com.cxhc.system.service.hcdoc.impl;

import com.cxhc.common.utils.DateUtils;
import com.cxhc.system.domain.hcdoc.HdProjectInfo;
import com.cxhc.system.domain.hcdoc.HdSmallBell;
import com.cxhc.system.domain.hcdoc.HdUserInfo;
import com.cxhc.system.mapper.hcdoc.HcDocUserMapper;
import com.cxhc.system.mapper.hcdoc.HdApprovalFormMapper;
import com.cxhc.system.mapper.hcdoc.HdProjectInfoMapper;
import com.cxhc.system.mapper.hcdoc.HdSmallBellMapper;
import com.cxhc.system.service.hcdoc.HdSmallBellService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


@Service
public class HdSmallBellServiceImpl implements HdSmallBellService {


    @Autowired
    private HdSmallBellMapper hdSmallBellMapper;

    @Autowired
    private HdApprovalFormMapper hdApprovalFormMapper;

    @Autowired
    private HcDocUserMapper hcDocUserMapper;

    @Autowired
    private HdProjectInfoMapper hdProjectInfoMapper;

    @Override
    public HdSmallBell selectById(String id) {
        return hdSmallBellMapper.selectById(id);
    }

    @Override
    public int insert(HdSmallBell hdSmallBell) {
        return hdSmallBellMapper.insert(hdSmallBell);
    }

    @Override
    public List<Map<String,String>> selectList(String userId) {
        HdUserInfo hdUserInfo = hcDocUserMapper.selectuserInfoById(userId);
        List<Map<String,String>> listMap = new ArrayList<>();
        if ("admin".equals(hdUserInfo.getRoleName())){
            List<Map<String, String>> maps = hdApprovalFormMapper.selectListForSmallBell();
            if (!CollectionUtils.isEmpty(maps)){
                for (Map<String, String> map : maps) {
                    String noticeContent = map.get("userName") + " 提交了一份," + map.get("formTypeName") + "申请";
                    map.put("noticeContent",noticeContent);
                    map.put("type","1");
                    listMap.add(map);

                }
            }
        }
        String dateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, new Date());
        HdProjectInfo hdProjectInfoTemp = new HdProjectInfo();
        hdProjectInfoTemp.setNextPayDate(dateStr);
        hdProjectInfoTemp.setManagerUserId(userId);
        hdProjectInfoTemp.setStat("0");
        List<HdProjectInfo> hdProjectInfos = hdProjectInfoMapper.selectListByNextPayNode(hdProjectInfoTemp);

        for (HdProjectInfo hdProjectInfo : hdProjectInfos) {
            String advanceNoticeDays = hdProjectInfo.getAdvanceNoticeDays();
            int advanceNoticeDay = Integer.parseInt("-" + advanceNoticeDays);
            String nextPayDate = hdProjectInfo.getNextPayDate();
            Date date = DateUtils.dateTime(DateUtils.YYYY_MM_DD, nextPayDate);
            Date advanceNoticeDate = DateUtils.addDays(date, advanceNoticeDay);
            if (advanceNoticeDate.compareTo(new Date()) <= 0){
                Map<String,String> map = new HashMap<>();
                map.put("createTime",DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS));
                map.put("projectName",hdProjectInfo.getProjectName());
                String noticeContent = hdProjectInfo.getProjectName() + "项目合同款需要在" + nextPayDate + "前支付,请及时处理!";
                map.put("noticeContent",noticeContent);
                map.put("type","2");
                listMap.add(map);
            }
        }

        return listMap;
    }

    @Override
    public int update(HdSmallBell hdSmallBell) {
        return hdSmallBellMapper.update(hdSmallBell);
    }
}
